package com.qf.web;

import com.qf.model.User;
import com.qf.model.UserCard;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;


@WebServlet("/userServlet")
public class UserServlet extends BaseServlet{

    private UserService userService = new UserServiceImpl();

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        String code = req.getParameter("code");
        String attribute = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        if(!code.equalsIgnoreCase(attribute)){
            req.setAttribute("msg","验证码错误");
            req.setAttribute("user",user);
            req.getRequestDispatcher("/user/login.jsp").forward(req,resp);
        }
        User user1 = userService.login(user);
        if(user1!=null){
            List<UserCard> usercads =  userService.findAllCard(user1);//查询银行卡列表，将该用户银行卡存储
            user1.setUserCards(usercads);
            BigDecimal allCardMoney = userService.findAllCardMoney(usercads);  //查询用户所有银行卡余额
            BigDecimal allMoney = new BigDecimal(0);
            allMoney = allCardMoney.add(user1.getMoney());
            req.getSession().setAttribute( "allMoney",allMoney);
            req.getSession().setAttribute("user",user1);
            resp.sendRedirect("/homepage/home_page.jsp");
        }else {
            req.setAttribute("msg","手机号或密码错误");
            req.setAttribute("user",user1);
            req.getRequestDispatcher("/error/error.jsp").forward(req,resp);
        }

    }

    public void register(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            User user = new User();
            BeanUtils.populate(user,parameterMap);
            String code = req.getParameter("code");
            String attribute = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
            if(!code.equalsIgnoreCase(attribute)){
                req.setAttribute("msg","验证码错误");
                req.setAttribute("user",user);
                req.getRequestDispatcher("/user/regist.jsp").forward(req,resp);
            }

            int row = userService.regist(user);
            if(row>0){
                req.getSession().setAttribute("user",user);
                resp.sendRedirect("/account/account.jsp");
            }else {
                req.setAttribute("msg","系统正忙，请稍后重试");
                req.setAttribute("user",user);
                resp.sendRedirect("/user/regist.jsp");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


}
